if (!require("pacman")) install.packages("pacman")
pacman::p_load("weights", "interactions", "cjoint", "plm", "interactions", "jtools", "stats", "miceadds", "broom", "RColorBrewer", "ggstatsplot", "ggpubr", "stargazer", "sandwich", "hrbrthemes", "rms", "interplot", "coefplot", "gmodels", "car", "lattice","foreign", "ggplot2", "MASS", "Hmisc", "reshape2", "oddsratio", "tidyr", "psych", "dplyr", "tidyverse", "ggthemes")

ds <- read.csv("Gubitz.protests.ds.csv", stringsAsFactors = F)

ds <- ds %>% filter(Finished == 1)
ds <- ds %>% filter(consent == 1)
ds <- ds %>% filter(race %in% c("black", "white"))

table(ds$race)


#mean(ds.true$Duration..in.seconds.)

#IQR.total <- IQR(ds$Duration..in.seconds.)
#quantile(ds$Duration..in.seconds.)

#Q3 <- 989
#Q1 <- 443

#slowpokes <- ((IQR.total*1.5)+Q3)
#speeders <- (Q1-(IQR.total*1.5))

#get rid of outliers
#ds.true <- ds %>% filter(Duration..in.seconds. < slowpokes)

#demographics
#building 7-point partisanship
ds$piddem.re <- car::recode(ds$piddem, "1=1; 2=2; else = 0")
ds$pidrep.re <- car::recode(ds$pidrep, "1=7; 2=6; else = 0")
ds$pidlean.re <- car::recode(ds$pidlean, "1=3; 2=5; 3=4; else = 0")

ds$pid7 <- ds$piddem.re + ds$pidrep.re + ds$pidlean.re
ds$pid7 <- car::recode(ds$pid7, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else = NA")
table(ds$pid7)

table(ds$age)
#age, where 35-44 is 1, 35-45 is 2, 45-64 is 3, 65-84 is 4, and 85+ is 5
ds$age.demo <- cut(ds$age, c(17,34,44,66,84,88), labels = c(1:5))

ds.white <- dplyr::filter(ds, race == "white")
ds.black <- dplyr::filter(ds, race == "black")
#gender
prop.table(table(ds.black$gender))
prop.table(table(ds.white$gender))
#age
prop.table(table(ds.black$age.demo))
prop.table(table(ds.white$age.demo))
#income
prop.table(table(ds.black$income))
prop.table(table(ds.white$income))
#education
prop.table(table(ds.black$education))
prop.table(table(ds.white$education))
#partyID
prop.table(table(ds.black$pid7))
prop.table(table(ds.white$pid7))



ds$age <- nalevs(ds$age)
ds$income <- nalevs(ds$income)
ds$education <- nalevs(ds$education)
ds$pid7.01 <- nalevs(ds$pid7)
ds$ideology <- nalevs(ds$ideology)

#sdo scale
#In order to make this into one variable representing "SDO", some variables need to be flipped such that positive values = greater legitimacy
ds <- ds %>% mutate(sdo1 = 6 - sdo1, 
                    sdo2 = 6 - sdo2, 
                    sdo6 = 6 - sdo6, 
                    dominance = nalevs(sdo1 + sdo2 + sdo3 + sdo4 + sdo5 + sdo6 + sdo7 + sdo8))
table(ds.white$dominance)



#social dominance orientation alpha = 0.83
dfsdo <- dplyr::select(ds, sdo1, sdo2, sdo3, sdo4, sdo5, sdo6, sdo7, sdo8)
psych::alpha(dfsdo)

#linked fate
ds$linkedfate1 <- car::recode(ds$linkedfate1, "1=0; 2=1; else = 0")
ds$linkedfate2 <- car::recode(ds$linkedfate2, "1=5; 2=4; 3=3; 4=2; else = 0")
ds$linkedfate <- ds$linkedfate1 + ds$linkedfate2
ds$linkedfate <- car::recode(ds$linkedfate, "1=1; 2=2; 3=3; 4=4; 5=5; else = NA")
ds$linkedfate <- nalevs(ds$linkedfate)
table(ds$linkedfate)

ds$condition <- as.factor(ds$condition)

ds$picket.uncivil <- nalevs(ds$picket.uncivil)
ds$chant.uncivil <- nalevs(ds$chant.uncivil)
ds$march.uncivil <- nalevs(ds$march.uncivil)
ds$punch.uncivil <- nalevs(ds$punch.uncivil)
ds$interrupt.uncivil <- nalevs(ds$interrupt.uncivil)
ds$parade.uncivil <- nalevs(ds$parade.uncivil)
ds$knee.uncivil <- nalevs(ds$knee.uncivil)
ds$petition.uncivil <- nalevs(ds$petition.uncivil)
ds$property.uncivil <- nalevs(ds$property.uncivil)
ds$throw.uncivil <- nalevs(ds$throw.uncivil)
ds$sitin.uncivil <- nalevs(ds$sitin.uncivil)
ds$blocktraffic.uncivil <- nalevs(ds$blocktraffic.uncivil)
ds$occupy.uncivil <- nalevs(ds$occupy.uncivil)
ds$graffiti.uncivil <- nalevs(ds$graffiti.uncivil)
ds$camp.uncivil <- nalevs(ds$camp.uncivil)
ds$lawsuit.uncivil <- nalevs(ds$lawsuit.uncivil)
ds$boycott.uncivil <- nalevs(ds$boycott.uncivil)
ds$vigil.uncivil <- nalevs(ds$vigil.uncivil)
ds$threat.uncivil <- nalevs(ds$threat.uncivil)
ds$leaflet.uncivil <- nalevs(ds$leaflet.uncivil)

ds$picket.participate <- nalevs(ds$picket.participate)
ds$chant.participate <- nalevs(ds$chant.participate)
ds$march.participate <- nalevs(ds$march.participate)
ds$punch.participate <- nalevs(ds$punch.participate)
ds$interrupt.participate <- nalevs(ds$interrupt.participate)
ds$parade.participate <- nalevs(ds$parade.participate)
ds$knee.participate <- nalevs(ds$knee.participate)
ds$petition.participate <- nalevs(ds$petition.participate)
ds$property.participate <- nalevs(ds$property.participate)
ds$throw.participate <- nalevs(ds$throw.participate)
ds$sitin.participate <- nalevs(ds$sitin.participate)
ds$blocktraffic.participate <- nalevs(ds$blocktraffic.participate)
ds$occupy.participate <- nalevs(ds$occupy.participate)
ds$graffiti.participate <- nalevs(ds$graffiti.participate)
ds$camp.participate <- nalevs(ds$camp.participate)
ds$lawsuit.participate <- nalevs(ds$lawsuit.participate)
ds$boycott.participate <- nalevs(ds$boycott.participate)
ds$vigil.participate <- nalevs(ds$vigil.participate)
ds$threat.participate <- nalevs(ds$threat.participate)
ds$leaflet.participate <- nalevs(ds$leaflet.participate)

write.csv(ds, "Gubitz_clean_ds.csv")